clear all
set more off
set matsize 10000
tempfile ubigeo temp


local lista trust_police trust_army trust_church trust_newsp trust_rtv trust_parties trust_regi trust_jury trust_parliament trust_jne trust_onpe
local controls schooling male rural native married head 

use mergedp, clear
merge 1:1 conglome vivienda hogar codperso year using `working'/ri
drop _merge

** LABELS
label var todoAT "Violence Exposure"

******* Results
drop if vote_matter == .
local lista vote_matter voted voted_blank voted_null partN dem_works dem_matters1 gov_demo1 gov_auto1

local r = 1
foreach var in `lista' {
	replace `var' = `var' * 100 if `r' != 5
	reghdfe `var' preescA primarA secondA tertiaA preescT primarT secondT tertiaT `controls', abs(ubigeo1993 ubigeonac1993 i.ubigeo1993#i.ubigeonac1993 i.age i.year i.provid#c.trend) vce(cluster ubigeonac1993) keepsin
	local BpreescA = _b[preescA]
	local BprimarA = _b[primarA]
	local BsecondA = _b[secondA]
	local BtertiaA = _b[tertiaA]	
	local BpreescT = _b[preescT]
	local BprimarT = _b[primarT]
	local BsecondT = _b[secondT]
	local BtertiaT = _b[tertiaT]
	local pvpreescA = 0
	local pvprimarA = 0
	local pvsecondA = 0
	local pvtertiaA = 0	
	local pvpreescT = 0
	local pvprimarT = 0
	local pvsecondT = 0
	local pvtertiaT = 0	
	forvalues p = 1/200 {
		qui reghdfe `var' preescA`p' primarA`p' secondA`p' tertiaA`p' preescT`p' primarT`p' secondT`p' tertiaT`p' `controls', abs(ubigeo1993 ubigeonac1993 i.ubigeo1993#i.ubigeonac1993 i.age i.year i.provid#c.trend) vce(cluster ubigeonac1993) keepsin
		if abs(_b[preescA`p']) > abs(`BpreescA') {
			local ++pvpreescA
		}
		if abs(_b[primarA`p']) > abs(`BprimarA') {
			local ++pvprimarA
		}
		if abs(_b[secondA`p']) > abs(`BsecondA') {
			local ++pvsecondA
		}
		if abs(_b[tertiaA`p']) > abs(`BtertiaA') {
			local ++pvtertiaA
		}
		if abs(_b[preescT`p']) > abs(`BpreescT') {
			local ++pvpreescT
		}
		if abs(_b[primarT`p']) > abs(`BprimarT') {
			local ++pvprimarT
		}
		if abs(_b[secondT`p']) > abs(`BsecondT') {
			local ++pvsecondT
		}
		if abs(_b[tertiaT`p']) > abs(`BtertiaT') {
			local ++pvtertiaT
		}

		di `pvpreescA' "," `pvpreescT' ";" `pvprimarA' "," `pvprimarT' ";" `pvsecondA' "," `pvsecondT' ";" `pvtertiaA' "," `pvtertiaT' " / " `p'
	}
		local npreescA`r' = (round(`pvpreescA'/200, 0.0001)) 
		local nprimarA`r' = (round(`pvprimarA'/200, 0.0001)) 
		local nsecondA`r' = (round(`pvsecondA'/200, 0.0001)) 
		local ntertiaA`r' = (round(`pvtertiaA'/200, 0.0001)) 
		local npreescT`r' = (round(`pvpreescT'/200, 0.0001)) 
		local nprimarT`r' = (round(`pvprimarT'/200, 0.0001)) 
		local nsecondT`r' = (round(`pvsecondT'/200, 0.0001)) 
		local ntertiaT`r' = (round(`pvtertiaT'/200, 0.0001)) 
	local ++r
}

texdoc init Table_10b.tex, replace
tex & `npreescA1' & `npreescA2' & `npreescA3' & `npreescA4' & `npreescA5' & `npreescA6' & `npreescA7' & `npreescA8' & `npreescA9' \\
tex & `nprimarA1' & `nprimarA2' & `nprimarA3' & `nprimarA4' & `nprimarA5' & `nprimarA6' & `nprimarA7' & `nprimarA8' & `nprimarA9' \\
tex & `nsecondA1' & `nsecondA2' & `nsecondA3' & `nsecondA4' & `nsecondA5' & `nsecondA6' & `nsecondA7' & `nsecondA8' & `nsecondA9' \\
tex & `ntertiaA1' & `ntertiaA2' & `ntertiaA3' & `ntertiaA4' & `ntertiaA5' & `ntertiaA6' & `ntertiaA7' & `ntertiaA8' & `ntertiaA9' \\
tex & `npreescT1' & `npreescT2' & `npreescT3' & `npreescT4' & `npreescT5' & `npreescT6' & `npreescT7' & `npreescT8' & `npreescT9' \\
tex & `nprimarT1' & `nprimarT2' & `nprimarT3' & `nprimarT4' & `nprimarT5' & `nprimarT6' & `nprimarT7' & `nprimarT8' & `nprimarT9' \\
tex & `nsecondT1' & `nsecondT2' & `nsecondT3' & `nsecondT4' & `nsecondT5' & `nsecondT6' & `nsecondT7' & `nsecondT8' & `nsecondT9' \\
tex & `ntertiaT1' & `ntertiaT2' & `ntertiaT3' & `ntertiaT4' & `ntertiaT5' & `ntertiaT6' & `ntertiaT7' & `ntertiaT8' & `ntertiaT9' \\
